package com.bankcomm.ui.views;

import com.bankcomm.R;
import com.bankcomm.common.ConfigManager;
import com.bankcomm.common.LPMid;
import com.bankcomm.util.LPUtils;
import com.rytong.tools.utils.LPToastUtil;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

/**
 * 底部的Tab控件，用到地方比较多
 * @author cheng.l 2013-6-14 下午01:19:11
 */
public class LPBottomBar extends LinearLayout{

	private LinearLayout rootView;
	private int currentIndex = 0;
	public LPBottomBar(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init();
	}

	public LPBottomBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init();
	}

	private void init() {
		// TODO Auto-generated method stub
		rootView = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.layout_bottom_bar, null);
		LPUtils.scaledTHeight(rootView);
		
		int size = rootView.getChildCount();
		for(int i = 0;i < size;i++){
			final int index = i;
			rootView.getChildAt(i).setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					LPToastUtil.getInstance().isBranchBank=false;
					setSelectTab(index);
					gotoHome(index);
				}
			});
		}
		this.addView(rootView,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
		
		rootView.getChildAt(0).setBackgroundResource(R.drawable.button_03);
	}

	/**
	 * 跳转是切换到首页的那个频道
	 * @param index
	 */
	private void gotoHome(int index) {
		// TODO Auto-generated method stub
		if(null != ConfigManager.currentView_){
			ConfigManager.currentView_.finish();			
		}
		LPMid.getInstance().um_.backToHome((Activity)getContext(), index);
	}
	
	/**
	 * 设置当前选中的那个
	 */
	public void setSelectTab(int index){
		if(currentIndex != index){
			rootView.getChildAt(index).setBackgroundResource(R.drawable.button_03);			
			rootView.getChildAt(currentIndex).setBackgroundResource(R.drawable.bottom_tab_bg);
			currentIndex = index;
		}
	}
}
